﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;

using $prjsn$.Common.Data.Services;
using $prjsn$.Entity.Services;

namespace $safeprojectname$.Services
{
    public class ArticleFilterValidator : FilterValidatorBase<Article, ArticleFilter, int>
    {
        protected override void Init(RuleCollection<ArticleFilter> rules) { base.Init(rules); }
    }
    public class ArticleValidator : EntityValidatorBase<Article, int, IArticleDao>
    {
        protected override void Init(RuleCollection<Article> rules)
        {
            // references
            rules.Require(entity => entity.ArticleCategory);
            rules.Require(entity => entity.ArticleGroup);

            // dates
            rules.For(entity => entity.LastUpdateDate).RequireValueBetween(DateTime.Now.Date, DateTime.Now.Date.AddDays(1));
            rules.For(entity => entity.CreationDate).RequireValueBetween(DateTime.Now.Date, DateTime.Now.Date.AddDays(1))
                .WhenCreating();

            // string
            rules.For(entity => entity.Title).RequireLengthBetween(2, 250);
            rules.For(entity => entity.Description).RequireLengthBetween(2, 700);
            rules.For(entity => entity.Text).RequireLengthBetween(0, 32000);

            // TODO expression rule
            // AppendAddUpdate((f, entity) => f.CheckRegularExpression(entity.Code, "[^a-zA-Z0-9-_]", false), true);

            // unique, user typed ID
            rules.For(entity => entity.Code).RequireLengthBetween(2, 150)
                .BreakOnError();
            rules.IsUnique(entity => Dao.Find(entity.ID, new ArticleFilter { CodeExact = entity.Code }), "Code")
                .BreakOnError();
        }
    }
}
